﻿using System;
using System.Collections.Generic;
using System.Linq;
using Gp.GCX;
using Gp.Scripts.Core;
using Gp.Scripts.Data;
using QFSW.QC;
using QFSW.QC.Actions;

// ReSharper disable ExplicitCallerInfoArgument

namespace Gp.Scripts.Console {
    [CommandPrefix("battle.")]
    public static class ConsoleApiBattle {
        public class UnitSelectItem {
            public int Index;
            // public CellSide Side;
            public BaseUnit Unit;

            // public UnitSelectItem(int index, CellSide side, BaseUnit unit) {
            //     Index = index;
            //     Side = side;
            //     Unit = unit;
            // }
            //
            // public override string ToString() {
            //     string side = Side == CellSide.Left ? "L" : "R";
            //
            //
            //     return $"[{side}{Index + 1}]: {Unit.UnitName}";
            // }
        }


        // [Command("applyBuff")]
        // public static IEnumerator<ICommandAction> ApplyBuff(string buffId) {
        //     var model = MainArchitecture.Interface.GetModel<RulesModel>();
        //     if (model == null) {
        //         yield return new Value("battle has not init yet.");
        //         yield break;
        //     }
        //
        //     var data = model.GetDataById(buffId);
        //
        //
        //     if (data is not BuffData buffData) {
        //         yield return new Value("wrong buff id : " + buffId);
        //         yield break;
        //     }
        //
        //     List<UnitSelectItem> lefts = new List<UnitSelectItem>();
        //
        //
        //     // 选择单位
        //     for (int i = 0; i < 4; i++) {
        //         var lu = CellGridSystem.Instance.PlaceHolder.GetUnit(CellSide.Left, i);
        //         if (lu != null) {
        //             lefts.Add(new UnitSelectItem(i, CellSide.Left, lu));
        //         }
        //     }
        //
        //
        //     // 选择单位
        //     for (int i = 0; i < 4; i++) {
        //         var ru = CellGridSystem.Instance.PlaceHolder.GetUnit(CellSide.Right, i);
        //         if (ru != null) {
        //             lefts.Add(new UnitSelectItem(i, CellSide.Right, ru));
        //         }
        //     }
        //
        //
        //     yield return new Choice<UnitSelectItem>(lefts, unit => { unit.Unit.AddBuff(buffData.BuildBuff()); });
        //
        //
        //     yield return new Value("Success");
        // }

        // [Command("healUnit")]
        // public static IEnumerator<ICommandAction> HealUnit(string pos) {
        //     if (pos.Length > 2) {
        //         yield return new Value("Wrong syntax");
        //         yield break;
        //     }
        //
        //     var arr = pos.ToCharArray();
        //
        //     var placeHolder = CellGridSystem.Instance.PlaceHolder;
        //     if (placeHolder == null) {
        //         yield return new Value("not battling");
        //         yield break;
        //     }
        //
        //     BaseUnit unit = null;
        //     var num = arr[1] - '0' - 1;
        //     switch (arr[0]) {
        //         case 'l':
        //             unit = placeHolder.GetUnit(CellSide.Left, num);
        //             if (unit == null) yield break;
        //             BehaviourSystem.Instance.HealUnit(unit);
        //             break;
        //         case 'r':
        //             unit = placeHolder.GetUnit(CellSide.Right, num);
        //             if (unit == null) yield break;
        //             BehaviourSystem.Instance.HealUnit(unit);
        //             break;
        //         default:
        //             yield return new Value("Wrong syntax");
        //             yield break;
        //     }
        //
        //
        //     yield return new Value("Success");
        // }


        [Command("recoverAction")]
        public static IEnumerator<ICommandAction> RecoverAction(int id, int amount = 2) {
            BaseUnit unit = Global.Get<BattleSystem>().BattleSettle.GetUnitsByPlayer(PlayerConst.PlayerUnitNumber).FirstOrDefault();
            if (unit == null) yield break;

            CostField type = (CostField)id;
            unit.ComCost.AddResource(type, amount);
            yield return new Value("Success");
        }


        [Command("end")]
        public static IEnumerator<ICommandAction> EndBattle() {
            try {
                MainArchitecture.Interface.SendCommand(new BattleEndCommand(Global.Get<BattleSystem>().BattleSettle));
            }
            catch (Exception e) { }

            yield return new Value("Success");
        }
    }
}